package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.tb.lmt.domain.bo.TbMutualInspectionBo;
import com.ruoyi.tb.lmt.domain.vo.TbMutualInspectionVo;
import com.ruoyi.tb.lmt.mapper.TbLmtrepairWorkdetailMapper;
import com.ruoyi.tb.lmt.mapper.TbMutualInspectiondetailMapper;
import com.ruoyi.tb.lmt.service.ITbMutualInspectionService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TbSpecialInspectionBo;
import com.ruoyi.tb.lmt.domain.vo.TbSpecialInspectionVo;
import com.ruoyi.tb.lmt.domain.TbSpecialInspection;
import com.ruoyi.tb.lmt.mapper.TbSpecialInspectionMapper;
import com.ruoyi.tb.lmt.service.ITbSpecialInspectionService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 专检主表Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-04
 */
@RequiredArgsConstructor
@Service
public class TbSpecialInspectionServiceImpl implements ITbSpecialInspectionService {

    private final TbSpecialInspectionMapper baseMapper;

    @Autowired
    public TbLmtrepairWorkdetailMapper tbLmtrepairWorkdetailMapper;

    @Autowired
    public TbSpecialInspectionMapper tbSpecialInspectionMapper;

    @Autowired
    public TbMutualInspectiondetailMapper tbMutualInspectiondetailMapper;

    /**
     * 查询专检主表
     */
    @Override
    public TbSpecialInspectionVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询专检主表列表
     */
    @Override
    public TableDataInfo<TbSpecialInspectionVo> queryPageList(TbSpecialInspectionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbSpecialInspection> lqw = buildQueryWrapper(bo);
        Page<TbSpecialInspectionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询专检主表列表
     */
    @Override
    public List<TbSpecialInspectionVo> queryList(TbSpecialInspectionBo bo) {
        LambdaQueryWrapper<TbSpecialInspection> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbSpecialInspection> buildQueryWrapper(TbSpecialInspectionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbSpecialInspection> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPlanId() != null, TbSpecialInspection::getPlanId, bo.getPlanId());
        lqw.eq(bo.getLmtRepairId() != null, TbSpecialInspection::getLmtRepairId, bo.getLmtRepairId());
        lqw.like(StringUtils.isNotBlank(bo.getPlanName()), TbSpecialInspection::getPlanName, bo.getPlanName());
        lqw.eq(StringUtils.isNotBlank(bo.getLmtType()), TbSpecialInspection::getLmtType, bo.getLmtType());
        lqw.eq(StringUtils.isNotBlank(bo.getLmtNumber()), TbSpecialInspection::getLmtNumber, bo.getLmtNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkerTeam()), TbSpecialInspection::getWorkerTeam, bo.getWorkerTeam());
        lqw.eq(StringUtils.isNotBlank(bo.getRepairType()), TbSpecialInspection::getRepairType, bo.getRepairType());
        lqw.eq(StringUtils.isNotBlank(bo.getRepairTimes()), TbSpecialInspection::getRepairTimes, bo.getRepairTimes());
        lqw.eq(StringUtils.isNotBlank(bo.getRepairCode()), TbSpecialInspection::getRepairCode, bo.getRepairCode());
        lqw.eq(StringUtils.isNotBlank(bo.getRepairChar()), TbSpecialInspection::getRepairChar, bo.getRepairChar());
        lqw.eq(bo.getRepairStartDate() != null, TbSpecialInspection::getRepairStartDate, bo.getRepairStartDate());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskAllCount()), TbSpecialInspection::getTaskAllCount, bo.getTaskAllCount());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskFinishCount()), TbSpecialInspection::getTaskFinishCount, bo.getTaskFinishCount());
        lqw.eq(StringUtils.isNotBlank(bo.getLegacyIssuesCount()), TbSpecialInspection::getLegacyIssuesCount, bo.getLegacyIssuesCount());
        lqw.eq(bo.getSendToUserTime() != null, TbSpecialInspection::getSendToUserTime, bo.getSendToUserTime());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkUser()), TbSpecialInspection::getWorkUser, bo.getWorkUser());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkGroup()), TbSpecialInspection::getWorkGroup, bo.getWorkGroup());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TbSpecialInspection::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkNeed()), TbSpecialInspection::getWorkNeed, bo.getWorkNeed());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkNeed2()), TbSpecialInspection::getWorkNeed2, bo.getWorkNeed2());
        lqw.eq(bo.getCreateTime() != null, TbSpecialInspection::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), TbSpecialInspection::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getUpdateTime() != null, TbSpecialInspection::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), TbSpecialInspection::getUpdateBy, bo.getUpdateBy());
        return lqw;
    }

    /**
     * 新增专检主表
     */
    @Override
    public Boolean insertByBo(TbSpecialInspectionBo bo) {
        TbSpecialInspection add = BeanUtil.toBean(bo, TbSpecialInspection.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改专检主表
     */
    @Override
    public Boolean updateByBo(TbSpecialInspectionBo bo) {
        TbSpecialInspection update = BeanUtil.toBean(bo, TbSpecialInspection.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbSpecialInspection entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除专检主表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TbSpecialInspectionBo selectByBo(TbSpecialInspectionBo bo) {
        Long lmtRepairId = bo.getLmtRepairId();
        TbSpecialInspectionBo queryBo = new TbSpecialInspectionBo();
        queryBo.setLmtRepairId(lmtRepairId);
        queryBo.setPlanId(bo.getPlanId());
        queryBo.setPlanName(bo.getPlanName());
        queryBo.setLmtType(bo.getLmtType());
        queryBo.setLmtNumber(bo.getLmtNumber());
        queryBo.setWorkerTeam(bo.getWorkerTeam());
        queryBo.setRepairType(bo.getRepairType());
        queryBo.setRepairTimes(bo.getRepairTimes());
        queryBo.setRepairCode(bo.getRepairCode());
        queryBo.setRepairChar(bo.getRepairChar());
        queryBo.setRepairStartDate(bo.getRepairStartDate());
        //queryBo.setWorkGroup(bo.getWorkGroup());
        queryBo.setTaskAllCount(tbLmtrepairWorkdetailMapper.selectTaskAllCount(lmtRepairId));
        queryBo.setTaskFinishCount(tbLmtrepairWorkdetailMapper.selectFinishAllCount(lmtRepairId));
        queryBo.setLegacyIssuesCount(tbLmtrepairWorkdetailMapper.selectLegacyIssuesCount(lmtRepairId));
        queryBo.setWorkNeed(tbMutualInspectiondetailMapper.selectMutualByLmt(lmtRepairId));
        queryBo.setWorkNeed2("0");
        LoginUser currentUser =  LoginHelper.getLoginUser();
        queryBo.setCreateBy(currentUser.getUsername());
        queryBo.setCreateTime(new Date());
        //修改状态跳过审批逻辑，直接进行操作
        //queryBo.setStatus("待审批");
        queryBo.setStatus("专检中");
        return queryBo;
    }

    @Override
    public TableDataInfo<TbSpecialInspectionVo> selectBySpecial(String id, String lmtType, String lmtNumber, String status, String repairChar, String planName, String workGroup, String workUser, PageQuery pageQuery) {
        List<TbSpecialInspectionVo> result = tbSpecialInspectionMapper.selectBySpecial(id,lmtType,lmtNumber,status,repairChar,planName,workGroup,workUser);
        //分页
        if (pageQuery.getPageSize()!= null && pageQuery.getPageNum()!= null) {
            List<TbSpecialInspectionVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }

    @Override
    public String updateWorkNeedByBo(Long id) {
        return tbSpecialInspectionMapper.selectSpecialId(id);
    }
}
